﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Oracle.DataAccess.Client;
using System.Collections;

namespace TP2
{
    public partial class PhysicalClient : Form
    {
        OracleConnection con;
        bool flag = false;
        DataTable physicTable = new DataTable("phys");
        DataTable loanTable = new DataTable("loan");
        
        


        public  PhysicalClient()
        {
            InitializeComponent();
                
        }

        private void ClearList()
        {
            
        }

        public void refreshTable()
        {
            this.ClearList();
            DBWorkClass dbWork = new DBWorkClass();
            PhysicalClientTypeClass selectMass = new PhysicalClientTypeClass();
            selectMass = dbWork.SelectFromPhysicalClient();
            int j = 0;
            if (selectMass.idClient.Count == 0)
            {
                dataGridView1.RowCount = 1;
                dataGridView1[0, 0].Value = "";
                dataGridView1[1, 0].Value = "";
                dataGridView1[2, 0].Value = "";
                dataGridView1[3, 0].Value = "";

            }
            else { dataGridView1.RowCount = selectMass.idClient.Count; }
            while (j < selectMass.idClient.Count)
            {
                dataGridView1[0, j].Value = (selectMass.fio[j].ToString());
                dataGridView1[1, j].Value = selectMass.date[j].ToString();
                dataGridView1[2, j].Value = selectMass.pasport[j].ToString();
                dataGridView1[3, j].Value = selectMass.income[j].ToString();
                j++;
            }
        }

        private void PhysicalClient_Load(object sender, EventArgs e)
        {
            this.refreshTable();         
        }

        private void FillTable()
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();          
        }

        

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddPhysicalClientForm addForm = new AddPhysicalClientForm(this);
            addForm.Show();
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DBWorkClass dbWork = new DBWorkClass();
                PhysicalClientTypeClass selectMass = new PhysicalClientTypeClass();
                selectMass = dbWork.SelectFromPhysicalClient();
                int index = dataGridView1.CurrentRow.Index;
                int id_client = Convert.ToInt32(selectMass.idClient[index]);
                dbWork.DeleteFromPhisycalClient(id_client);
                this.refreshTable();
            }
            catch { MessageBox.Show("ошибка"); }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnAccount.Enabled = true;          
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                DBWorkClass dbWork = new DBWorkClass();
                PhysicalClientTypeClass selectMass = new PhysicalClientTypeClass();
                selectMass = dbWork.SelectFromPhysicalClient();
                int index = dataGridView1.CurrentRow.Index;
                int id_client = Convert.ToInt32(selectMass.idClient[index]);
                EditPhysicalClientForm editFr = new EditPhysicalClientForm(Convert.ToInt32(id_client), selectMass.fio[index].ToString(), Convert.ToDateTime(selectMass.date[index]), selectMass.pasport[index].ToString(), Convert.ToInt32(selectMass.income[index]), this);
                editFr.Show();
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }

        private int GetIdClient()
        {
            DBWorkClass dbWork = new DBWorkClass();
            PhysicalClientTypeClass selectMass = new PhysicalClientTypeClass();
            selectMass = dbWork.SelectFromPhysicalClient();
            int index = dataGridView1.CurrentRow.Index;
            int id_client = Convert.ToInt32(selectMass.idClient[index]);
            return id_client;
        }

        private void btnAccount_Click(object sender, EventArgs e)
        {
            try
            {
                if (comboBox1.SelectedIndex == 1)
                {
                    AccumulationForm accumForm = new AccumulationForm(this.GetIdClient(), "физический");
                    accumForm.Show();
                }
                else
                {
                    LoanForm loanForm = new LoanForm(this.GetIdClient(), "физический");
                    loanForm.Show();
                }
            }
            catch (Exception) { MessageBox.Show("ошибка"); }
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            
        }
    }
}

